package Gui;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import Command.MainCommand;
import Observing.ObserverThread;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class InitingSys extends org.eclipse.swt.widgets.Composite {
	private Composite allView;
	private static Button stop;
	private static ProgressBar bar;
	private Button cancel;
	private Button start;
	static Shell shell;
	static Label end;
	private Button continueBut;
	static String stringForEnd="";
	private static Thread thread;
	
	private static MainCommand mainCommand;
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	 * @param mainCommand 
	*/
	public static void showGUI(MainCommand mainCmd) {
		Display display = Display.getDefault();
		shell = new Shell(display);
		mainCommand=mainCmd;
		InitingSys inst = new InitingSys(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		
		thread = new UsingFlagToShutdownThread(shell , mainCommand, bar,stringForEnd,end); 
		
		stop.setEnabled(false);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public InitingSys(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}
	public static class UsingFlagToShutdownThread extends Thread {
		int bekilled=1;
		private Shell shell ;
		MainCommand mainCommand;
		private static ProgressBar bar;
		private Label end;
		String stringForEnd;
		
		public UsingFlagToShutdownThread(Shell sl,MainCommand mainCmd,ProgressBar bar, String str,Label endLabel){
			shell=sl;
			mainCommand=mainCmd;
			this.bar=bar;
			stringForEnd=str;
			end=endLabel;
		}

		
			public void run() {
			    while(  (mainCommand.returnPrecentInit()<99) && (bekilled!=0) ) 
			    {
			    	if(bekilled!=2)
			    	{
				    		final int value = mainCommand.returnPrecentInit();
					    	try {
					    			Thread.sleep(50);
					    			
					    	} catch (Exception e) { }
					    	shell.getDisplay().asyncExec(new Runnable(){
					    		
							        public void run() {
							        	bar.setSelection(value);
							         }
					    	});
				    }
			    	
			    	
			    }
			    
			    stringForEnd="---ENDED!!!--- ";
			    System.out.println(stringForEnd);
			    Display.getDefault().asyncExec(new Runnable() {
			    	public void run()
			    	{
			    		end.setText("---ENDED!!!--- ");
			    	}
			    });
			    
		  }
		  public void die() {
				bekilled=0;
				
				
		  }
		  public void pause() {
				bekilled=2;
				
				
		  }
		  
		  public void cont() {
				bekilled=1;
				
				
		  }
	}
	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			{
				allView = new Composite(this, SWT.NONE);
				FormLayout allViewLayout = new FormLayout();
				FormData allViewLData = new FormData();
				allViewLData.left =  new FormAttachment(0, 1000, 0);
				allViewLData.top =  new FormAttachment(0, 1000, 0);
				allViewLData.width = 328;
				allViewLData.height = 196;
				allView.setLayoutData(allViewLData);
				allView.setLayout(allViewLayout);
				{
					end = new Label(allView, SWT.NONE);
					FormData endLData = new FormData();
					endLData.left =  new FormAttachment(0, 1000, 125);
					endLData.top =  new FormAttachment(0, 1000, 107);
					endLData.width = 60;
					endLData.height = 20;
					end.setLayoutData(endLData);
					end.setText(stringForEnd);
				
				}

				{
					FormData barLData = new FormData();
					barLData.left =  new FormAttachment(0, 1000, 40);
					barLData.top =  new FormAttachment(0, 1000, 82);
					barLData.width = 255;
					barLData.height = 19;
					bar = new ProgressBar(allView, SWT.NONE);
					bar.setLayoutData(barLData);
					//bar.redraw(5, 10, 10, 10, true);
					bar.setState(50);
				}
				{
					cancel = new Button(allView, SWT.PUSH | SWT.CENTER);
					FormData cancelLData = new FormData();
					cancelLData.left =  new FormAttachment(0, 1000, 195);
					cancelLData.top =  new FormAttachment(0, 1000, 146);
					cancelLData.width = 92;
					cancelLData.height = 28;
					cancel.setLayoutData(cancelLData);
					cancel.setText("cancel");
					cancel.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							
							((UsingFlagToShutdownThread) thread).die();
							thread.stop();
							mainCommand.stopInit();
							shell.dispose();
						}
					});
				}
				{
					start = new Button(allView, SWT.PUSH | SWT.CENTER);
					FormData startLData = new FormData();
					startLData.left =  new FormAttachment(0, 1000, 35);
					startLData.top =  new FormAttachment(0, 1000, 26);
					startLData.width = 74;
					startLData.height = 27;
					start.setLayoutData(startLData);
					start.setText("start");
					start.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							stop.setEnabled(true);
							if(thread.isAlive())
							{
								((UsingFlagToShutdownThread) thread).cont();
								
							}
							else
							{
								((UsingFlagToShutdownThread) thread).die();
								thread = new UsingFlagToShutdownThread(shell , mainCommand, bar,stringForEnd,end); 
								thread.start();
							}
							mainCommand.reStartInit();
							
						}
					});
				}
				{
					stop = new Button(allView, SWT.PUSH | SWT.CENTER);
					FormData stopLData = new FormData();
					stopLData.left =  new FormAttachment(0, 1000, 218);
					stopLData.top =  new FormAttachment(0, 1000, 26);
					stopLData.width = 69;
					stopLData.height = 27;
					stop.setLayoutData(stopLData);
					stop.setText("pause");
					stop.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							if(thread.isAlive())
							{
								((UsingFlagToShutdownThread) thread).pause();
								stop.setEnabled(false);
								mainCommand.pauseInit();
							}
							
						}
					});
				}
				
				
					

				
				
			}
			this.layout();
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
